<template>
  <el-card class="box-card" v-loading="isPublishing">
    <div slot="header" class="clearfix">
      <span>写文章</span>
      <el-button style="float: right; padding: 3px 0" type="text" @click="publishBlog" icon='el-icon-s-promotion'>发布</el-button>
    </div>
    <el-row :gutter="10">
      <el-col :span="6">
        <el-upload
            name="blogAvatars"
            class="avatar-uploader"
            :action="globalApi.uploadImg"
            :show-file-list="false"
            :on-success="handleAvatarSuccess">
          <img v-if="blogInfo.cover" :src="blogInfo.cover" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon">封面</i>
        </el-upload>
      </el-col>
      <el-col :span="18">
        <el-row :gutter="10">
          <el-col :span="24">
            <el-input v-model="blogInfo.title" placeholder="请输入文章标题"/>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="24">
            <el-input v-model="blogInfo.description" placeholder="请输入文章简介"/>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="24">
            <el-tag
                :key="tag"
                v-for="tag in blogInfo.tags"
                closable
                :disable-transitions="false"
                @close="handleClose(tag)">
              {{ tag }}
            </el-tag>
            <el-input
                class="input-new-tag"
                v-if="inputVisible"
                v-model="inputValue"
                ref="saveTagInput"
                size="small"
                @keyup.enter.native="handleInputConfirm"
                @blur="handleInputConfirm"
            >
            </el-input>
            <el-button v-else class="button-new-tag" size="small" @click="showInput">+ 新标签</el-button>
          </el-col>
        </el-row>
      </el-col>
    </el-row>
    <el-row :gutter="10" style="margin-top: 20px;">
      <el-col :span="24">
        <div style="border: 1px solid #ccc;">
          <Toolbar
              style="border-bottom: 1px solid #ccc"
              :editor="edit.editor"
              :defaultConfig="edit.toolbarConfig"
              :mode="edit.mode"
          />
          <Editor
              style="height:800px;"
              :defaultConfig="edit.editorConfig"
              v-model="edit.html"
              :mode="edit.mode"
              @onCreated="onCreated"
          />
        </div>
      </el-col>
    </el-row>
  </el-card>
</template>

<script>
import globalApi from '@/config/globalApi'
import {Editor, Toolbar} from '@wangeditor/editor-for-vue'
import editorConfig from '@/config/editorConfig'
import BlogService from '@/service/blogs/BlogService'

export default {
  name: "ArticleEditor",
  components: {Editor, Toolbar},
  data() {
    return {
      globalApi,
      blogInfo: {
        title: '',
        description: '',
        tags: [],
        cover: ''
      },
      isPublishing: false,
      inputVisible: false,
      inputValue: '',
      edit: {
        editor: null,
        html: '',
        toolbarConfig: {
          excludeKeys: [
            'editVideoSize',
            'insertVideo',
            'uploadVideo'
          ]
        },
        editorConfig,
        mode: 'default', // or 'simple',
      }
    }
  },
  beforeDestroy() {
    const editor = this.edit.editor
    if (editor == null) return
    editor.destroy() // 组件销毁时，及时销毁编辑器
  },
  methods: {
    handleAvatarSuccess(res) {
      this.blogInfo.cover = res.data.imgList[0]
    },

    handleClose(tag) {
      this.blogInfo.tags.splice(this.blogInfo.tags.indexOf(tag), 1);
    },

    showInput() {
      this.inputVisible = true;
      this.$nextTick(() => {
        this.$refs.saveTagInput.$refs.input.focus();
      })
    },

    handleInputConfirm() {
      let inputValue = this.inputValue;
      if (inputValue) {
        this.blogInfo.tags.push(inputValue);
      }
      this.inputVisible = false;
      this.inputValue = '';
    },

    // 编辑器的生命周期钩子
    onCreated(editor) {
      this.edit.editor = Object.seal(editor) // 一定要用 Object.seal() ，否则会报错
    },

    // 发布文章
    publishBlog() {
      let blogData = {
        title: this.blogInfo.title,
        tags: this.blogInfo.tags,
        description: this.blogInfo.description,
        cover: this.blogInfo.cover,
        content: this.edit.editor.getHtml()
      }

      // 数据校验 简单校验 细节懒得写了
      if (blogData.title && blogData.tags && blogData.description && blogData.content) {

        this.isPublishing = true
        BlogService.createBlog(blogData).then(res => {
          if(res.data.status === 200){
            this.$message.success(res.data.message)

            setTimeout(()=>{
              this.$emit('publishSuccess')
            },1500)

          }else {
            this.$message.error(res.data.message)
          }
        }).finally(() => {
          this.isPublishing = false
        })

      } else {
        this.$message.warning('请完善文章信息！')
      }
    }
  }
}
</script>

<style src="@wangeditor/editor/dist/css/style.css"></style>
<style scoped>
>>> .avatar-uploader .el-upload {
  border: 1px dashed #8c939d;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

>>> .avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

>>> .avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 260px;
  height: 150px;
  line-height: 150px;
  text-align: center;
}

>>> .avatar {
  width: 260px;
  height: 150px;
  display: block;
}

>>> .el-tag + .el-tag {
  margin-left: 10px;
}

>>> .button-new-tag {
  margin-left: 10px;
  height: 32px;
  line-height: 30px;
  padding-top: 0;
  padding-bottom: 0;
}

>>> .input-new-tag {
  width: 90px;
  margin-left: 10px;
  vertical-align: bottom;
}

>>> .el-input {
  margin-bottom: 20px;
}

>>> .w-e-text-container .w-e-scroll::-webkit-scrollbar {
  width: 4px;
  height: 4px;
}
</style>
